package com.yunquan.ohana.ui.home.frames.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R
import com.yunquan.ohana.entity.FramesEntity

enum class FramesType {
    FRAMES,
    CALENDAR,
}

class FramesAdapter(private val type: FramesType) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mTitleView = R.layout.item_frames_group
    private val mItemView = R.layout.item_frames2

    private val mListItems = mutableListOf<FramesEntity>()

    private var onShareListener: ((item: FramesEntity) -> Unit)? = null

    fun setOnShareListener(listener: (item: FramesEntity) -> Unit) {
        this.onShareListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 1) {
            val view = View.inflate(parent.context, mTitleView, null)
            return TitleViewHolder(view)
        } else {
            val view = View.inflate(parent.context, mItemView, null)
            return ItemViewHolder(view, type, onShareListener)
        }
    }

    override fun getItemCount(): Int {
        return mListItems.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (mListItems[position].id == null) 1 else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = mListItems[position]
        if (holder is TitleViewHolder) {
            holder.bind(item)
        } else if (holder is ItemViewHolder) {
            holder.bind(item)
        }
    }

    fun getItem(position: Int): FramesEntity {
        return mListItems[position]
    }

    fun setData(list: List<FramesEntity>) {
        mListItems.clear()
        mListItems.addAll(list)
        mListItems.sortedByDescending { e -> if (e.online == true) 1 else 0 }

        var title: FramesEntity? = null
        var i = 0
        var size = mListItems.size
        while (i < size) {
            val item = mListItems[i]
            if (title == null || title.online != item.online) {
                title = FramesEntity(null, null, null, item.online)
                mListItems.add(i, title)
                size += 1
            }
            i++
        }
        notifyDataSetChanged()
    }

    inner class TitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvGroupName = itemView.findViewById<TextView>(R.id.tv_group_name)

        fun bind(item: FramesEntity) {
            val redId = if (item.online == true) R.string.online else R.string.offline
            val color = if (item.online == true) R.color.c_54 else R.color.c_ffff3535
            tvGroupName.setTextColor(ContextCompat.getColor(itemView.context, color))
            tvGroupName.setText(redId)
        }
    }

    inner class ItemViewHolder(
        itemView: View,
        private val type: FramesType,
        private val onShare: ((item: FramesEntity) -> Unit)?,
    ) : RecyclerView.ViewHolder(itemView) {
        private val tvFramesName = itemView.findViewById<TextView>(R.id.tv_frames_name)
        private val tvRole = itemView.findViewById<TextView>(R.id.tv_frames_role)
        private val btnShare = itemView.findViewById<TextView>(R.id.btn_share)
        private val btnCheck = itemView.findViewById<ImageView>(R.id.btn_check)

        init {
            btnCheck.visibility = View.GONE
            btnShare.visibility = View.VISIBLE
        }

        fun bind(item: FramesEntity) {
            tvFramesName.text = item.name
            if (item.check) {
                btnCheck.setImageResource(R.mipmap.ic_check2_true)
            } else {
                btnCheck.setImageResource(R.mipmap.ic_check2_false)
            }

            if (type == FramesType.CALENDAR) {
                btnShare.setText(R.string.enter)
                tvRole.visibility = View.VISIBLE
                if (item.deviceRoleVo != null) {
                    tvRole.text = "Profile name: ${item.deviceRoleVo!!.name}"
                } else {
                    tvRole.setText(R.string.add_your_profile_to_share_family_events)
                }
            } else {
                btnShare.setText(R.string.share)
                tvRole.visibility = View.GONE
            }

            btnShare.setOnClickListener {
                onShare?.invoke(item)
            }
        }
    }
}